package com.sright.bcard.enterprise.web.context;

import com.alibaba.ttl.TransmittableThreadLocal;

public class RequestStorager {

    final private static ThreadLocal<ServletContext> requestContext = new TransmittableThreadLocal<>();

    public static Long getUserId() {
        ServletContext servletContext = requestContext.get();
        if (servletContext == null) {
            return null;
        }
        return servletContext.getUserId();
    }

    public static void setUserId(Long userId) {
        ServletContext servletContext = requestContext.get();
        if (servletContext == null) {
            servletContext = new ServletContext();
            requestContext.set(servletContext);
        }
        servletContext.setUserId(userId);
    }

    public static String getToken() {
        ServletContext servletContext = requestContext.get();
        if (servletContext == null) {
            return null;
        }
        return servletContext.getToken();
    }

    public static void setToken(String token) {
        ServletContext servletContext = requestContext.get();
        if (servletContext == null) {
            servletContext = new ServletContext();
            requestContext.set(servletContext);
        }
        servletContext.setToken(token);
    }
}
